/*
"Replication Data for: The real-time impact of the war on Russian imports: 
a synthetic control method approach" by Mancini, Conteduca, Borin 

Fig4-5.do: The files export the dta files underlying Figures 4 and 5 of the 
paper 

*/

clear  
cd "${main_loc}"



forvalues s=1/13 {

  

 
use  "${main_loc}/data_sect`s'_WTR",  clear

************************************************************************************************************************
**# LOOP
************************************************************************************************************************

drop if partner=="Ukraine"	
drop if partner=="Turkey"	
drop if partner=="Kazakhstan"	
drop if partner=="Belarus"	
drop if partner=="Serbia"	
drop if partner=="Armenia"
drop if partner=="Uzbekistan"
drop if partner=="Kyrgyzstan"
drop if partner=="Georgia"
drop if partner=="Azerbaijan"

levelsof iso2_o, local(levels) 

foreach c of local levels{
	
preserve
capture graph close

keep if iso2_o=="`c'"
di "`c'"
replace expshare=0 if expshare==.


egen id=group(iso2_d),label
egen t=group(date)
tsset id t

drop if BVlndist==.
drop if BVcontiguity==.
drop if BVcommon_language==.
drop if lnagdp_wdi_const==.
drop if lnavg_outp==.

tsfill,full
replace value=0 if value==.

sort id t
local varl  BVlndist BVcontiguity BVcommon_language lnagdp_wdi_const  max_agree_pta_goods max_agree_cu lnavg_outp lnavg_outptrade
foreach x of local varl {  
sort id `x' 
by id: carryforward `x' ,replace
}

 
sort id t
gen date_new=date  
order id date date_new 

bys id: replace date_new=l.date_new+1 if date_new==.
bys id: replace date_new=l2.date_new+2 if date_new==.
bys id: replace date_new=l3.date_new+3 if date_new==.
bys id: replace date_new=l4.date_new+4 if date_new==.
bys id: replace date_new=l5.date_new+5 if date_new==.
bys id: replace date_new=f.date_new-1 if date_new==.
bys id: replace date_new=f2.date_new-2 if date_new==.
bys id: replace date_new=f3.date_new-3 if date_new==.
bys id: replace date_new=f4.date_new-4 if date_new==.
bys id: replace date_new=f5.date_new-5 if date_new==.
format date_new %tm
drop date fdate year  month
rename date_new date

gen fdate = dofm(date)
gen year=year(fdate)
gen month=month(fdate)
gen dm=month
   


 
 keep if t>=13

sort id t

summ id if partner=="Russia"
capture   synth   value   value BVlndist BVcontiguity BVcommon_language  max_agree_pta_goods  expshare lnavg_outp  ,   trunit("`r(mean)'") trperiod(38)    keep("`c'_Russia_s`s'",replace) 
 summ id if partner=="Russia"
capture synth_asmd, trunit("`r(mean)'") trperiod(38) depvar(value) data("`c'_Russia_s`s'.dta")
sca `c'_Russia_mean=r(mean)
sca test=r(mean)
gen soglia=0.4
sca `c'_Russia_max=r(max)
sum soglia
if test>r(mean) {
capture erase "`c'_Russia_s`s'.dta"  
 }
 
 restore
}


 


 foreach c of local levels {   
    clear 
capture use "${main_loc}/`c'_Russia_s`s'.dta"
capture keep _Y_treated _Y_synthetic _time
capture gen iso2_o="`c'"
capture drop if iso2_o==""
capture drop if _time==.

capture save  "${main_loc}/`c'_pre_append_Russia_s`s'.dta",replace
}
}




clear
cd "${main_loc}/"
 forvalues s=1/13 {

local theFiles: dir . files "*_pre_append_Russia_s`s'.dta" 
append using `theFiles' 
gen keep=1 if _time==48
bys iso2_o: egen insample=max(keep)
drop if insample==.
drop if _time>48
egen id=group(iso2_o)
tsset id _time
drop if iso2_o=="UA"
capture drop period sector
gen period=0 if _time>=39 & _time<=40
replace period=1 if _time>=46 & _time<=48
drop if period==.
gen sector=`s'
rename iso2_o iso2
merge n:1 iso2 using "${main_loc}/sanctioning_list_WTR.dta" 
drop if _m == 2
drop _m
replace sanctioning=0 if sanctioning==.
collapse (sum) _Y_treated _Y_synthetic,by(sector period sanctioning)

save "${main_loc}/CountrySector_results_s`s'.dta",replace
tempfile temp_output
        save `temp_output'
		if(`s'==1){
			save  "${main_loc}/CountrySector_results_s_all.dta",replace
		}
	else{
		clear	
		use  "${main_loc}/CountrySector_results_s_all.dta", clear
		append using `temp_output'
		save  "${main_loc}/CountrySector_results_s_all.dta" , replace
		clear
	}
	

 }
 
 
 
 

 
*FIGURE 4 
use  "${main_loc}/CountrySector_results_s_all.dta",clear
merge n:n sector using "${main_loc}/hs2_to_sect_WTR.dta"
drop _m hs2* def
collapse (sum) _Y*,by(period sector sector_name)
g deltap= _Y_treated/_Y_synthetic*100-100
save "${main_loc}/Figure_4.dta",replace

 
 
*FIGURE 5 
use  "${main_loc}/CountrySector_results_s_all.dta",clear
merge n:n sector using "${main_loc}/hs2_to_sect_WTR.dta"
drop _m hs2* def
keep if sector == 11 | sector == 10 | sector == 9 
keep if period==1
replace _Y_treated=_Y_treated/1000000000
replace _Y_synthetic=_Y_synthetic/1000000000
gen diff=_Y_treated -_Y_synthetic 
replace diff=diff/3
keep diff sector_name
save "${main_loc}/Figure_5.dta",replace



 
